home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / mac / What's New? / Development Kits / Mac OS / Multiprocessing 2.1 SDK / Sample Code / HappyTrails ƒ / µApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-30  |  8.0 KB  |  456 lines  |  [TEXT/CWIE]

  1. /**\
  2. |**|    µApp.c
  3. \**/
  4.  
  5. /**\
  6. |**|    compiler directives
  7. \**/
  8.  
  9. #define SystemSevenOrLater        1
  10.  
  11. /**\
  12. |**|    Includes
  13. \**/
  14.  
  15. #include <Devices.h>
  16. #include <Fonts.h>
  17. #include <CodeFragments.h>
  18. #include <Dialogs.h>
  19. #include <DiskInit.h>
  20. #include <LowMem.h>
  21. #include <fp.h>
  22. #include <PLStringFuncs.h>
  23. #include <Sound.h>
  24. #include <Traps.h>
  25. #include <Threads.h>
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29.  
  30. #include "HappyTrails.h"
  31.  
  32. /**\
  33. |**|    typedefs, structs, enums, etc.
  34. \**/
  35.  
  36. enum
  37. {
  38.     mAppleMenu = 128,
  39.         iAboutBox = 1,
  40.     mFileMenu = 129,
  41.         iQuit = 1,
  42.     mTasksMenu = 130,
  43.     mWeightMenu = 131
  44. };
  45.  
  46. /**\
  47. |**|    private (local) function prototypes
  48. \**/
  49.  
  50. static OSErr Init_Mac(void);
  51. static void Handle_Command(long ms);
  52. static void Handle_Event(const EventRecord *pEventPtr);
  53. static void Handle_NullEvent(const EventRecord *pEventPtr);
  54. static void Handle_MouseEvent(const EventRecord *pEventPtr);
  55. static void Handle_KeyEvent(char key,SInt16 modifiers);
  56. static void Handle_UpdateEvent(WindowPtr updateWindowP);
  57. //static void Handle_ActivateEvent(WindowPtr updateWindowP);
  58. static void Handle_DiskEvent(long message);
  59. static void Handle_OSEvent(long message);
  60.  
  61. static Boolean SetUp_MenuBar(void);
  62. static void Adjust_MenuItems(void);
  63. static void Adjust_Cursor(const Boolean pContextual);
  64.  
  65. /**\
  66. |**|    globals & constants
  67. \**/
  68.  
  69. WindowPtr    gWindowPtr = nil;
  70. Rect        gWindowRect;
  71. Boolean        gInBackGround = false;
  72. Rect         gMenuRect = {0,0,0,0};
  73.  
  74. static Boolean gQuitFlag = false;
  75.  
  76. /**\
  77. |**|    main
  78. \**/
  79.  
  80. void main(void)
  81. {
  82.     if (!Init_Mac() && SetUp_MenuBar() && !HT_Init(9))
  83.     {
  84.         do
  85.         {
  86.             EventRecord event;
  87.  
  88.             WaitNextEvent(everyEvent,&event,-1,nil);
  89.             Adjust_Cursor((event.modifiers & controlKey) == controlKey);
  90.             Handle_Event(&event);
  91.         }
  92.         while (!gQuitFlag);
  93.  
  94.         HT_Term();
  95.     }
  96. }
  97.  
  98. /**\
  99. |**|    Initialize toolboxes
  100. \**/
  101.  
  102. static OSErr Init_Mac(void)
  103. {
  104.     OSErr        error;
  105.     SysEnvRec    theWorld;
  106.         
  107.     //
  108.     //    Test the computer to be sure we can do color.  
  109.     //    If not we would crash, which would be bad.  
  110.     //    If we can’t run, just beep and exit.
  111.     //
  112.  
  113.     error = SysEnvirons(1, &theWorld);
  114.     if (theWorld.hasColorQD == false)
  115.     {
  116.         SysBeep(50);
  117.         ExitToShell();                    // If no color QD, we must leave.
  118.     }
  119.     
  120.     MaxApplZone();
  121.     InitGraf(&(qd.thePort));
  122.     InitFonts();
  123.     InitWindows();
  124.     InitMenus();
  125.     TEInit();
  126.     InitDialogs(nil);
  127.  
  128.     InitContextualMenus();
  129.  
  130.     //
  131.     //    Make a new window for drawing in, and it must be a color window.  
  132.     //    The window is full screen size, made smaller to make it more visible.
  133.     //
  134.  
  135.     gWindowRect = qd.screenBits.bounds;
  136. //    InsetRect(&gWindowRect, 20, 20);
  137.     gWindowRect.top += LMGetMBarHeight() + 1;
  138.  
  139.     gWindowPtr = NewCWindow(nil, &gWindowRect, "\pHappy Trails!", true, documentProc, 
  140.                         (WindowPtr) -1, false, 0);
  141.  
  142.     OffsetRect(&gWindowRect, -gWindowRect.left, -gWindowRect.top);
  143.  
  144.     SetPort(gWindowPtr);                    // set window to current graf port
  145.     TextSize(kTextSize);                    // smaller font for drawing.
  146.  
  147.     return noErr;
  148. }
  149.  
  150. /**\
  151. |**|    Menu hooks
  152. \**/
  153.  
  154. static pascal short MyMBarHook(Rect *menuRect)
  155. {
  156. //    RgnHandle tempRgn = NewRgn();
  157.  
  158.     gMenuRect = *menuRect;
  159.  
  160. //    RectRgn(tempRgn,menuRect);
  161. //    CalcVisBehind(LMGetWindowList(),tempRgn);
  162. //    InvertRgn(tempRgn);
  163.  
  164.     //YieldToAnyThread();
  165. //    DisposeRgn(tempRgn);
  166.     return 0;
  167. }
  168.  
  169. static pascal void MyMenuHook(void)
  170. {    
  171. //    RgnHandle tempRgn = NewRgn();
  172. //    RectRgn(tempRgn,&gMenuRect);
  173. //    CalcVisBehind(LMGetWindowList(),tempRgn);
  174. //    InvertRgn(tempRgn);
  175.  
  176.     //YieldToAnyThread();
  177. //    DisposeRgn(tempRgn);
  178. }
  179.  
  180. /**\
  181. |**|    Setup menu bar
  182. \**/
  183.  
  184. static Boolean SetUp_MenuBar(void)
  185. {
  186.     Handle mBar = GetNewMBar(128);    // handle to menu bar resource
  187.  
  188.     if (!ResError() && mBar)
  189.     {
  190.         SetMenuBar(mBar);
  191.         AppendResMenu(GetMenuHandle(mAppleMenu),'DRVR');
  192.         DrawMenuBar();
  193.         ReleaseResource(mBar);
  194.         Adjust_MenuItems();
  195.  
  196.         LMSetMBarHook(NewMBarHookProc(MyMBarHook));
  197.         LMSetMenuHook(NewMenuHookProc(MyMenuHook));
  198.  
  199.         return true;
  200.     }
  201.     return false;
  202. }
  203.  
  204. /**\
  205. |**|    Adjust menu items
  206. \**/
  207.  
  208. static void Adjust_MenuItems(void)
  209. {
  210.     MenuHandle tMenuHdl = GetMenuHandle(mTasksMenu);
  211.     if (tMenuHdl)
  212.     {
  213.         UInt16 index,count = CountMItems(tMenuHdl);
  214.         for (index = 1;index <= count;index++)
  215.             CheckItem(tMenuHdl, index, index == gNumTasks);
  216.     }
  217. }
  218.  
  219. /**\
  220. |**|    Adjust cursor
  221. \**/
  222.  
  223. static void Adjust_Cursor(const Boolean pContextual)
  224. {
  225.     if (pContextual)
  226.     {
  227.         static CursHandle tCursHandle = nil;
  228.         if (!tCursHandle)
  229.             tCursHandle = GetCursor(128);    // kThemeContextualMenuArrowCursor?
  230.         if (tCursHandle)
  231.             SetCursor(*tCursHandle);
  232.     }
  233.     else
  234.         SetCursor(&qd.arrow);
  235. }
  236.  
  237. /**\
  238. |**|    Do menu command
  239. \**/
  240.  
  241. static void Handle_Command(long ms)
  242. {
  243.     short    menuID = ms >> 16,
  244.             menuItem = ms & 0xFFFF;
  245.  
  246.     switch (menuID)
  247.     {
  248.         case    mAppleMenu:
  249.             switch (menuItem)
  250.             {
  251.                 case iAboutBox:        // Bring up alert for About.
  252.                     SysBeep(15);
  253.                     break;
  254.                 default:            // All non-About items in this menu are DAs.
  255.                     {
  256.                         Str255    daName;
  257.                         GetMenuItemText(GetMenuHandle(menuID),menuItem,daName);
  258.                         OpenDeskAcc(daName);
  259.                     }
  260.                     break;
  261.             }
  262.             break;
  263.         case    mFileMenu:
  264.             switch (menuItem)
  265.             {
  266.                 case iQuit:
  267.                     gQuitFlag = true;
  268.                     break;
  269.                 default:
  270.                     SysBeep(15);
  271.                     break;
  272.             }
  273.             break;
  274.         case    mTasksMenu:
  275.             HT_Term();
  276.             HT_Init(menuItem);
  277.             break;
  278.         case    mWeightMenu:        // Weight Menu
  279.             break;
  280.     }
  281. }
  282.  
  283. /**\
  284. |**|    Do event
  285. \**/
  286.  
  287. static void Handle_Event(const EventRecord *pEventPtr)
  288. {
  289.     switch (pEventPtr->what)
  290.     {
  291.         case nullEvent:
  292.             Handle_NullEvent(pEventPtr);
  293.             break;
  294.         case mouseDown:
  295.             Handle_MouseEvent(pEventPtr);
  296.             break;
  297.         case keyDown:
  298.         case autoKey:
  299. dokey:
  300.             Handle_KeyEvent((char)(pEventPtr->message & charCodeMask),pEventPtr->modifiers);
  301.             break;
  302.         case updateEvt:
  303.             Handle_UpdateEvent((WindowPtr)pEventPtr->message);
  304.             break;
  305.         case diskEvt:
  306.             Handle_DiskEvent(pEventPtr->message);
  307.             break;
  308.         case osEvt:
  309.             Handle_OSEvent(pEventPtr->message);
  310.             break;
  311.         case kHighLevelEvent:
  312.             AEProcessAppleEvent(pEventPtr);
  313.             break;
  314.         default:
  315.             break;
  316.     }
  317. }
  318.  
  319. /**\
  320. |**|    Do null event
  321. \**/
  322.  
  323. static void Handle_NullEvent(const EventRecord *pEventPtr)
  324. {
  325.     (pEventPtr);    // #pragma unused (pEventPtr)
  326.     if (gWindowPtr)
  327.     {
  328.         Rect    tRect = {40,20,52,128};
  329.         const RGBColor blackRGBColor = {0,0,0}, yellowRGBColor = {255,255,0};
  330.         GrafPtr savePort;
  331.  
  332.         GetPort(&savePort);
  333.         SetPort(gWindowPtr);
  334.  
  335.         HT_DoNull();
  336.  
  337.         SetPort(savePort);
  338.     }
  339. }
  340.  
  341. /**\
  342. |**|    Do mousedown event
  343. \**/
  344.  
  345. static void Handle_MouseEvent(const EventRecord *pEventPtr)
  346. {
  347.     Boolean tInBackGround = gInBackGround;
  348.     WindowPtr            window;
  349.     short                part;
  350.  
  351.     gInBackGround = true;    // suspend drawing while menus are displayed
  352.     HT_DoNull();
  353.  
  354.     part = FindWindow(pEventPtr->where,&window);
  355.  
  356.     Adjust_Cursor(part == inContent);
  357.  
  358.     switch(part)
  359.     {
  360.         case inContent:
  361.             {
  362.                 if (window != FrontWindow())
  363.                     SelectWindow(window);
  364.                 else
  365.                     HT_DoClick(pEventPtr);
  366.             }
  367.             break;
  368.  
  369.         case inMenuBar:        // Process mouse menu command (if any).
  370.             {
  371.                 long ms;
  372.  
  373.                 Adjust_MenuItems();
  374.                 ms = MenuSelect(pEventPtr->where);
  375.                 if (ms)
  376.                     Handle_Command(ms);
  377.                 HiliteMenu(0);        // Unhighlight what MenuSelect hilited.
  378.             }
  379.             break;
  380.  
  381.         case inSysWindow:    // Let the system handle the mouseDown.
  382.             SystemClick(pEventPtr,window);
  383.             break;
  384.  
  385.         default:
  386.             break;
  387.     }
  388.     gInBackGround = tInBackGround;
  389.  
  390.     SetRect(&gMenuRect, 0, 0, 0, 0);
  391. }
  392.  
  393. /**\
  394. |**|    Handle key events
  395. \**/
  396.  
  397. static void Handle_KeyEvent(char key,SInt16 modifiers)
  398. {
  399.     if ((modifiers & cmdKey) != 0)
  400.     {
  401.         Adjust_MenuItems();
  402.         Handle_Command(MenuKey(key));
  403.         HiliteMenu(0);        // Unhighlight what MenuSelect hilited.
  404.     }
  405. }
  406.  
  407. /**\
  408. |**|    Handle update events
  409. \**/
  410.  
  411. static void Handle_UpdateEvent(WindowPtr updateWindowP)
  412. {
  413.     GrafPtr savePort;
  414.  
  415.     GetPort(&savePort);
  416.     SetPort(updateWindowP);
  417.     BeginUpdate(updateWindowP);
  418.  
  419.     UpdateControls(updateWindowP,updateWindowP->visRgn);
  420.  
  421.     DrawGrowIcon(updateWindowP);
  422.     EndUpdate(updateWindowP);
  423.  
  424.     SetPort(savePort);
  425. }
  426.  
  427. /**\
  428. |**|    Handle disk events
  429. \**/
  430.  
  431. static void Handle_DiskEvent(long message)
  432. {
  433.     Point dialogLocation = {100,100};
  434.  
  435.     if ((message & 0xFFFF0000) != noErr)
  436.     {
  437.         DIBadMount(dialogLocation,message);
  438.     }
  439. }
  440.  
  441. /**\
  442. |**|    Handle OS events
  443. \**/
  444.  
  445. static void Handle_OSEvent(long message)
  446. {
  447.     if ((message >> 24) == suspendResumeMessage)
  448.     {
  449.         if ((message & resumeFlag) != 0)
  450.             gInBackGround = false;
  451.         else
  452.             gInBackGround = true;
  453.     }
  454. }
  455.  
  456.